package com.gy.smart.home.view;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.smart.R;
import com.gy.smart.base.WebViewActivity;
import com.gy.smart.home.bean.HomeNewsBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.Urls;

import java.util.List;

/**
 * Created by 高岳 on 2017/1/4.
 * Describe:
 */
public class HomeNewsView extends LinearLayout {

    private LinearLayout ll1,ll2;
    private TextView tvTitle1,tvTitle2,tvTime1,tvTime2;
    private Handler handler;
    private boolean isShow;
    private int startY1, endY1, startY2, endY2;
    private Runnable runnable;
    private List<HomeNewsBean> list;
    private int position = 0;
    private int offsetY = 0;

    public HomeNewsView(Context context) {
        this(context,null);
    }

    public HomeNewsView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HomeNewsView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.view_home_news,this);
        ll1 = (LinearLayout) view.findViewById(R.id.fg_home_ll_news1);
        ll2 = (LinearLayout) view.findViewById(R.id.fg_home_ll_news2);
        tvTitle1 = (TextView) view.findViewById(R.id.fg_home_tv_news_title1);
        tvTitle2 = (TextView) view.findViewById(R.id.fg_home_tv_news_title2);
        tvTime1 = (TextView) view.findViewById(R.id.fg_home_tv_news_from1);
        tvTime2 = (TextView) view.findViewById(R.id.fg_home_tv_news_from2);
        offsetY = ll1.getMeasuredHeight();
        int type = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,66,context.getResources().getDisplayMetrics());
        if(offsetY == 0){
            offsetY = type;
        }
        handler = new Handler();

        runnable = new Runnable() {
            @Override
            public void run() {
                isShow = !isShow;

                if (position == list.size())
                    position = 0;

                if (isShow) {
                    int loc = position++;
                    tvTitle1.setText(list.get(loc).title);
                    tvTime1.setText("来源："+list.get(loc).author+"    "+list.get(loc).create_time);
                    tvTitle2.setText(list.get(loc+1 < list.size()?loc+1 : 0).title);
                    tvTime2.setText("来源："+list.get(loc+1 < list.size()?loc+1 : 0).author+"    "+list.get(loc+1 < list.size()?loc+1 : 0).create_time);
                } else {
//                    tvTitle2.setText(list.get(position++).title);
                    int loc = position++;
                    tvTitle2.setText(list.get(loc).title);
                    tvTime2.setText("来源："+list.get(loc).author+"    "+list.get(loc).create_time);
                    tvTitle1.setText(list.get(loc+1 < list.size()?loc+1 : 0).title);
                    tvTime1.setText("来源："+list.get(loc+1 < list.size()?loc+1 : 0).author+"    "+list.get(loc+1 < list.size()?loc+1 : 0).create_time);
                }

                startY1 = isShow ? 0 : offsetY;
                endY1 = isShow ? -offsetY : 0;
                startY2 = isShow ? offsetY : 0;
                endY2 = isShow ? 0 : -offsetY;

                ObjectAnimator.ofFloat(ll1, "translationY", startY1, endY1).setDuration(500).start();
                ObjectAnimator.ofFloat(ll2, "translationY", startY2, endY2).setDuration(500).start();

                handler.postDelayed(runnable, 4000);
            }
        };
        ll1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra(WebViewActivity.URL, Urls.web_url + list.get(position == list.size() ? 0 : position).id);
                context.startActivity(intent);
                AnimUtil.pushLeftInAndOut((Activity) context);
            }
        });
        ll2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra(WebViewActivity.URL, Urls.web_url + list.get(position == list.size() ? 0 : position).id);
                context.startActivity(intent);
                AnimUtil.pushLeftInAndOut((Activity) context);
            }
        });
    }

    public void setList(List<HomeNewsBean> list){
        this.list = list;
        if(list.size() > 0){
            tvTitle1.setText(list.get(0).title);
            tvTime2.setText("来源："+list.get(0).author+"    "+list.get(0).create_time);
            if(list.size() > 1){
                handler.post(runnable);
            }
        }
    }

    public void startScroll() {
        handler.post(runnable);
    }

    public void stopScroll() {
        handler.removeCallbacks(runnable);
    }
}
